package com.infoland.afanmall.model.my.dialog;

import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.blankj.utilcode.util.ToastUtils;
import com.infoland.afanmall.R;
import com.infoland.afanmall.base.dialog.OpenIsOk_2_Dialog;
import com.infoland.afanmall.base.entity.ResLoginEntity;
import com.infoland.afanmall.base.dialog.BaseNiceDialogFragment;
import com.infoland.afanmall.base.util.okhttp.OkHttpKit;
import com.infoland.afanmall.model.guiGe.entity.ViewHolder;
import com.infoland.afanmall.base.fragment.LoginDialogFragment;
import com.infoland.afanmall.base.util.CONST;
import com.infoland.afanmall.base.util.LogKit;
import com.infoland.afanmall.base.util.ToolsKit;
import com.infoland.afanmall.base.util.UserKit;
import com.infoland.afanmall.model.my.activity.FindPasswordActivity;
import com.infoland.afanmall.model.my.activity.PhoneRegisActivity;
import com.infoland.afanmall.model.my.fragment.MyCenterFragment;

import butterknife.BindView;
import okhttp3.Call;

/**
 * 手机登录 Dialog
 */
public class OpenPhone extends BaseNiceDialogFragment {

    @BindView(R.id.login_1_fanhui)
    LinearLayout login1Fanhui;
    @BindView(R.id.login_1_other)
    LinearLayout login1Other;
    @BindView(R.id.login_1_ed1)
    EditText login1Ed1;
    @BindView(R.id.login_1_yanzheng)
    LinearLayout login1Yanzheng;
    @BindView(R.id.login_1_ed2)
    EditText login1Ed2;
    @BindView(R.id.login_1_bt)
    LinearLayout login1Bt;
    @BindView(R.id.login_1_text2)
    TextView login1Text2;
    @BindView(R.id.login_1_1)
    LinearLayout login11;
    @BindView(R.id.login_2_fanhui)
    LinearLayout login2Fanhui;
    @BindView(R.id.login_2_other)
    LinearLayout login2Other;
    @BindView(R.id.login_2_ed1)
    EditText login2Ed1;
    @BindView(R.id.login_2_ed2)
    EditText login2Ed2;
    @BindView(R.id.login_2_bt)
    LinearLayout login2Bt;
    @BindView(R.id.login_2_text1)
    TextView login2Text1;
    @BindView(R.id.login_2_text2)
    TextView login2Text2;
    @BindView(R.id.login_2_2)
    LinearLayout login22;
    @BindView(R.id.login_1_txt1)
    TextView login1Txt1;
    int now_type = 1;
    String phone1;//验证码登录
    String yanzm1;//验证码登录
    String phone2;//密码登录
    String password2;//密码登录
    OpenIsOk_2_Dialog openDialog;
    public MyCenterFragment myCenterFragment;

    private void initData() {
        ToolsKit.setTypeNumber(login1Ed1);
        ToolsKit.setTypeNumber(login1Ed2);
        ToolsKit.setTypeNumber(login2Ed1);
        ToolsKit.setTypePassWord(login2Ed2);

        login11.setVisibility(View.VISIBLE); //初始展示验证码登录界面
        login22.setVisibility(View.GONE);//初始隐藏验证码登录界面
        /***验证码登录*/
        login1Bt.setOnClickListener(v -> {
            ToastUtils.showShort("验证码登录");
            phone1 = login1Ed1.getText().toString();
            yanzm1 = login1Ed2.getText().toString();
            if (ToolsKit.isPhone(phone1)) {
                if (ToolsKit.isYzM(yanzm1)) {
                    loginToYzmCode();
                } else {
                    ToastUtils.showShort("验证码格式错误");
                }
            } else {
                ToastUtils.showShort("电话号格式错误");

            }
        });
        /***获取验证码*/
        login1Yanzheng.setOnClickListener(v1 -> {
            phone1 = login1Ed1.getText().toString();
            if (ToolsKit.isPhone(phone1)) {
                getVerCode();
            } else {
                ToastUtils.showShort("手机号输入错误");
            }
        });
        /***密码登录*/
        login2Bt.setOnClickListener(v -> {
            ToastUtils.showShort("密码登录");
            phone2 = login2Ed1.getText().toString();
            password2 = login2Ed2.getText().toString();
            if (ToolsKit.isPhone(phone2)) {
                if (ToolsKit.isPassword(password2)) {
                    loginToPassWord();
                } else {
                    ToastUtils.showShort("密码格式错误");
                }
            } else {
                ToastUtils.showShort("手机号输入错误");
            }
        });
        /***展示找回密码Activity*/
        login2Text1.setOnClickListener(v -> {
            this.dismiss();
            ToolsKit.go(getActivity(), FindPasswordActivity.class);
        });
        /**展示密码登录界面**/
        login1Text2.setOnClickListener(v -> {
            now_type = 2;
            login22.setVisibility(View.VISIBLE);
            login11.setVisibility(View.GONE);
        });
        /**展示短信登陆界面**/
        login2Text2.setOnClickListener(v -> {
            now_type = 1;
            login11.setVisibility(View.VISIBLE);
            login22.setVisibility(View.GONE);

        });
        /***返回选择登录*/
        login1Fanhui.setOnClickListener(v -> {
            this.dismiss();
//            OpenShaiDan openShaiDan = new OpenShaiDan();
//            openShaiDan.setShowBottom(true);
//            openShaiDan.show(getFragmentManager());
            goLogin();
        });
        /***返回选择登录*/
        login2Fanhui.setOnClickListener(v -> {
            this.dismiss();
//            OpenShaiDan openShaiDan = new OpenShaiDan();
//            openShaiDan.setShowBottom(true);
//            openShaiDan.show(getFragmentManager());
            goLogin();
        });

//        login1Text2.seton
    }

    /***发送验证码登录请求*/
    private void loginToYzmCode() {
        JSONObject jsonObject = new JSONObject();
        LogKit.E(" yanzm1  ", "  " + yanzm1);
        jsonObject.put("phone", phone1);
//        flag	是	int	登陆方式 1验证码 2密码
        jsonObject.put("password", yanzm1);
        jsonObject.put("flag", 1);
        LogKit.E("jsonObject   ", "  " + jsonObject.toJSONString());
        new OkHttpKit().send(
                response200 -> {
                    loginOk(response200);
                    LogKit.E("response200 ---->   ", "  " + response200);
                },
                success401 -> {
                    LogKit.E("success401 ---->   ", "  " + success401);
                },
                error -> {
                    LogKit.E("error ---->   ", "  " + error);
                }
        ).post(CONST.USER_LOGIN, jsonObject, getActivity());


    }

    /***发送密码登录请求*/
    private void loginToPassWord() {
        String url = CONST.HTTP + CONST.USER_LOGIN;
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("phone", phone2);
//        flag	是	int	登陆方式 1验证码 2密码
        jsonObject.put("password", ToolsKit.getSHA1String(password2));
        jsonObject.put("flag", 2);
        LogKit.E("url   ", "  " + url);
        LogKit.E("jsonObject   ", "  " + jsonObject.toJSONString());


        new OkHttpKit().send(
                response200 -> {
                    LogKit.E("response   ", "  " + response200);
                    loginOk(response200);
                },
                success401 -> {
                    LogKit.E("success401 ---->   ", "  " + success401);
                },
                error -> {
                    LogKit.E("error ---->   ", "  " + error);
                }
        ).post(CONST.USER_LOGIN, jsonObject, getActivity());
    }

    /****登录请求成功*/
    private void loginOk(String response) {
        ResLoginEntity resLoginEntity = JSONObject.parseObject(response, ResLoginEntity.class);
        ResLoginEntity.ObjectBean object = resLoginEntity.getObject();
        if (resLoginEntity.isSuccess()) {
            UserKit.setUserCacheYes(object.getAccess_token(), object.getRefresh_token());
            loginOkGo();
        } else {
            String message = JSONObject.parseObject(response).getString("message");
            if ("此账号未注册".equals(message)) {
                String ss = "";
                if (now_type == 1) {
                    ss = phone1 + " 未注册";
                } else {
                    ss = phone2 + " 未注册";
                }
                openDialog = ToolsKit.openDialog_2(ss, "请检查是否输入错误，或者直接注册",
                        "取消", "直接注册", getActivity(),
                        v -> {
                            openDialog.dismiss();
                        },
                        v -> {
                            openDialog.dismiss();
                            goZhuCe();
                        }
                );
            } else {
                ToastUtils.showShort(message);
            }

        }
    }

    /***跳转到注册界面*/
    private void goZhuCe() {
        this.dismiss();
        ToolsKit.go(getActivity(), PhoneRegisActivity.class, null);
    }

    /****登录请求成功--跳转*/
    private void loginOkGo() {
        myCenterFragment.getkLogin();
        ToastUtils.showShort("登录成功");
        this.dismiss();
    }

    /****登录请求失败*/
    private void loginNo(Call call, Exception e) {
        LogKit.E(" e  ", "  " + e);
    }


    /***发送验证码*/
    private void getVerCode() {
//        phone	是	string	手机号码
//        type	是	int	验证码类型 1注册 2登陆 3忘记密码 4修改手机号码
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("phone", phone1);
        jsonObject.put("type", "2");
        UserKit.sendValidateCode(jsonObject, login1Yanzheng, login1Txt1,
                120 * 1000, 1000, "再次发送", getActivity());
    }

    /***回到登录Dialog*/
    private void goLogin() {
        LoginDialogFragment loginDialogFragment = new LoginDialogFragment();
        loginDialogFragment.setShowBottom(true);
//        loginDialogFragment.setOutCancel(false);
//        loginDialogFragment.setCancelable(false);
//        loginDialogFragment.getDialog().setCanceledOnTouchOutside(false);
        loginDialogFragment.show(getFragmentManager());
    }

    @Override
    public int intLayoutId() {
        return R.layout.open_login_phone;
    }

    @Override
    public void convertView(ViewHolder holder, BaseNiceDialogFragment dialog) {
        initData();
    }

    @Override
    public void onDestroy() {
        ToolsKit.deleteOKHttp(getActivity());
        super.onDestroy();
    }
}
